package com.example.lenovo.demoa;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {
    //:AUTHORITY 是自定义的主机名
    private static final String AUTHORITY = "cn.gdcp.djy";
    private static MySQLiteOpenHelper mySQLiteOpenHelper;
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        uriMatcher.addURI(AUTHORITY, "student", 1);
        uriMatcher.addURI(AUTHORITY, "score", 2);

    }

    @Override
    public boolean onCreate() {
        mySQLiteOpenHelper = new MySQLiteOpenHelper(getContext(), "stu.db", null, 1);
        return true;
    }


    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = mySQLiteOpenHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case 1:
                Cursor cursor1 = db.query("student", null, selection, selectionArgs, null, null, sortOrder);
                return cursor1;
            case 2:
                Cursor cursor2 = db.query("score", null, selection, selectionArgs, null, null, sortOrder);
                return cursor2;
        }
        return null;
    }


    @Override
    public String getType(Uri uri) {
        return null;
    }


    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case 1:
                db.insert("student", null, values);
                break;
            case 2:
                db.insert("score", null, values);
                break;
        }

        getContext().sendBroadcast(new Intent("STU_HAD_CHANGE"));
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case 1:
                int rows = db.delete("student", selection, selectionArgs);

                return rows;
            case 2:
                int row1 = db.delete("score", selection, selectionArgs);

                return row1;
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case 1:
                int rows = db.update("student", values, selection, selectionArgs);
                return rows;
            case 2:
                int row1 = db.update("score", values, selection, selectionArgs);
                return row1;
        }
        return 0;
    }
}
